package com.MedDroid.task;

import android.os.AsyncTask;
import android.view.View;
import com.MedDroid.activity.SchedulePageActivity;
import com.MedDroid.control.AuthenticationControl;
import com.MedDroid.control.DatabaseControl;
import com.MedDroid.entities.Permissions;

import java.lang.ref.WeakReference;
import java.sql.Date;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 02/12/13
 * Time: 7:13 PM
 * Description:
 */
public class LoadSchedulePageTask extends AsyncTask<Void, Void, ArrayList<Integer>>
{
	AuthenticationControl AuthenticationC;
	DatabaseControl DatabaseC;
	WeakReference<SchedulePageActivity> SchedulePage;
	Permissions Access;

	public LoadSchedulePageTask(AuthenticationControl Auth, DatabaseControl Data, SchedulePageActivity activity)
	{
		AuthenticationC = Auth;
		DatabaseC = Data;
		SchedulePage = new WeakReference<SchedulePageActivity>(activity);
	}

	@Override
	protected ArrayList<Integer> doInBackground(Void... voids)
	{
		Access = AuthenticationC.getAccess();
		return DatabaseC.getEvents(AuthenticationC.getLoginUser().getUserID());
	}

	@Override
	protected void onPostExecute(ArrayList<Integer> Events)
	{
		SchedulePageActivity Page = SchedulePage.get();
		if (!Access.getPermission(Permissions.PModifyAppointments))
		{
			Page.btnAddAppointment.setVisibility(View.GONE);
		}
		if (!Access.getPermission(Permissions.PRequestAppointments))
		{
			Page.btnRequestAppointment.setVisibility(View.GONE);
		}
		if (!Access.getPermission(Permissions.PViewAppointments))
		{
			//Page.btnViewAppointments.setVisibility(View.GONE);
		}
		if (Events.size() >= 1)
		{
			Page.ListNames = new ArrayList<String>();
			Page.ListEventID = Events;
			for (Integer I : Events)
			{
				String label = "";
				Date date = new Date(DatabaseC.getEvent(I).getDate());
				label = date.toString() + " ";
				label += DatabaseC.getEvent(I).getTitle();
				Page.ListNames.add(label);
			}
		}
		Page.makeList();
	}
}
